package com.op.teachingliteapp.fragment.viewer;

import org.vudroid.core.codec.CodecDocument;
import org.vudroid.core.codec.CodecPage;
import org.vudroid.pdfdroid.codec.PdfContext;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.RectF;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.LruCache;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageView;

import com.op.teachingliteapp.sender.ui.view.zoomgallery.ZoomImageView;


/**
 * Created by Genji on 2015/6/29.
 */

public class PPTAdapter extends BaseAdapter {
    private int pageCount;
    private PdfContext pdf_conext;
    private CodecDocument pdf_documen;
    private LruCache<Integer, ZoomImageView> zoomImageViewLruCache;
    private String uri;
    private int maxSize = 5 * 1024 * 1024;
    private Activity activity;
    private ImageView zoomImageView;
    private int m_GalleryHeight = 0;
    private int m_GalleryWidth = 0;
    private int width = 0;
    private int height = 0;


    public PPTAdapter(Activity activity, String uri, int m_GalleryWidth, int m_GalleryHeight) {
        this.activity = activity;
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager windowMgr = (WindowManager) this.activity.getSystemService(Context.WINDOW_SERVICE);
        windowMgr.getDefaultDisplay().getMetrics(dm);
        this.width = dm.widthPixels;
        this.height = dm.heightPixels;
        this.zoomImageViewLruCache = new LruCache<Integer, ZoomImageView>(maxSize);
        this.m_GalleryHeight = m_GalleryHeight;
        this.m_GalleryWidth = m_GalleryWidth;
        this.uri = uri;
        this.zoomImageView = new ZoomImageView(activity, this.m_GalleryWidth, this.m_GalleryHeight);
        this.pdf_conext = new PdfContext();
        this.pdf_documen = this.pdf_conext.openDocument(uri);
        this.pageCount = this.pdf_documen.getPageCount();
    }


    @Override
    public int getCount() {
        return this.pageCount;
    }

    @Override
    public ImageView getItem(int position) {
//        return getZoomImageView(position);
//        ZoomImageView zoomImageView = new ZoomImageView(this.activity, this.m_GalleryWidth, this.m_GalleryHeight);
//        zoomImageView.setImageResource(R.drawable.sun);
//        zoomImageView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//        switchZoomImageView(position);
        return this.zoomImageView;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        this.zoomImageView = new ZoomImageView(this.activity, this.m_GalleryWidth, this.m_GalleryHeight);

//        this.zoomImageView.setImageResource(R.drawable.sun);
//        setUpZoomImageView(position);
//        this.zoomImageView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


        return this.zoomImageView;
    }

    private void setUpZoomImageView(int index) {


        RectF rf = new RectF();
        rf.bottom = rf.right = (float) 1.0;
        CodecPage pdfPage = pdf_documen.getPage(index);
        Bitmap bitmap = pdfPage.renderBitmap(this.width, this.height, rf);


        zoomImageView.setImageBitmap(bitmap);
        Log.i("PPTAdapter", "i" + index + bitmap.getWidth() + " " + bitmap.getHeight());
//        zoomImageView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));


    }

}
